﻿using QG.JobBack.BLL.IService;
using QG.JobBack.DAL;
using QH.JobBack.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QG.JobBack.BLL.Service
{
    public class AdminService : BaseRepository<Admininfo>, IAdminService
    {
        /// <summary>
        /// 修改状态
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool UpdateStatus(Admininfo admininfo)
        {
            using (var db = new qgpingEntities())
            {
                Admininfo admininfos = db.Admininfo.FirstOrDefault(a => a.AdminId == admininfo.AdminId);
                admininfos.STATUS = admininfo.STATUS;
                return db.SaveChanges() > 0;
            }
        }
        /// <summary>
        /// 新增管理员信息
        /// </summary>
        /// <returns></returns>
        public bool AddAdmininfos(Admininfo admininfo)
        {
            admininfo.CreateTime = DateTime.Now;
            //代码执行完，则释放资源   声明一个实例
            using (var db = new qgpingEntities())
            {
                using (var tran = db.Database.BeginTransaction())
                {
                    try
                    {
                        db.Admininfo.Add(admininfo);
                        db.SaveChanges();
                    }
                    catch (Exception)
                    {
                        tran.Rollback();
                        return false;
                    }
                    tran.Commit();
                }
            }
            return true;
        }
        
        /// <summary>
        /// 修改管理员信息
        /// </summary>
        /// <param name="menu"></param>
        /// <returns></returns>
        public bool UpdateAdmininfos(Admininfo admininfo)
        {
            using (var db = new qgpingEntities())
            {
                Admininfo admininfos = db.Admininfo.FirstOrDefault(a => a.AdminId == admininfo.AdminId);
                admininfos.AdminName = admininfo.AdminName;
                admininfos.STATUS = admininfo.STATUS;
                admininfos.Sex = admininfo.Sex;
                admininfos.Phone = admininfo.Phone;
                admininfos.Email = admininfo.Email;
                return db.SaveChanges() > 0;
            }
        }

    }
}
